﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using LinqObj;
using System.Linq;
/// <summary>
/// Summary description for CampaignListBuilder
/// </summary>
/// 


public class CampaignListBuilder
{
    private IEnumerable<Campaign> campaignList;    
    public List<HyperLink> links;
    
	public CampaignListBuilder(IEnumerable<Campaign> campaignList)
	{
        this.campaignList = campaignList;
        this.links = new List<HyperLink>();
	}

    public Control BuildCampaignList()
    {
        HtmlGenericControl div1 = new HtmlGenericControl("div");
        div1.Attributes["class"] = "property_cat";
        div1.Attributes["style"] = "font-weight:normal; padding:0px; height:30px;";
        div1.InnerHtml = "<table width='100%' border='0' align='left' cellpadding='0' cellspacing='0'>";
        div1.InnerHtml += "<tr>";
        div1.InnerHtml += "<td width='22%'>Status</td>";
        div1.InnerHtml += "<td width='25%' align='left'>Agent Name</td>";
        div1.InnerHtml += "<td width='18%' align='left'>Campaign ID</td>";
        div1.InnerHtml += "<td width='14%' align='center'>Orders</td>";
        div1.InnerHtml += "<td width='6%' align='center'></td>";
        div1.InnerHtml += "</tr>";
        div1.InnerHtml += "</table><br/><br/>";

        //HtmlGenericControl divClear = new HtmlGenericControl("div");
        //divClear.Attributes["style"] = "clear:both;";
        //div1.Controls.AddAt(0, divClear);

        HtmlGenericControl containerDiv = new HtmlGenericControl("div");
        containerDiv.Attributes["Id"] = "campaignList";
        containerDiv.Attributes["style"] = "background-color:#333333;";
        HtmlGenericControl table = new HtmlGenericControl("table");
        table.Attributes["id"] = "campaign";
        
        foreach (Campaign _campaign in this.campaignList)
        {
            CampaingEntry entry = new CampaingEntry(_campaign);
            HtmlGenericControl div3 = new HtmlGenericControl("div3");
            div3.Visible = false;
            div3.Controls.Add(entry.GetEntry());
            //this.links.Add(entry.link);

            string[] textarray = entry.link.Text.Split(';');

            HtmlGenericControl div2 = new HtmlGenericControl("div");
            div2.Attributes["class"] = "pro_tbl";
            div2.InnerHtml = "<table width='100%' border='0' align='center' cellpadding='0' cellspacing='0'>";
            div2.InnerHtml += "<tr>";
            
            if(textarray[0].ToUpper() == "ACTIVE")
                div2.InnerHtml += "<td width='22%' align='left' class='activebf'>" + textarray[0] + "</td>";
            else if (textarray[0].ToUpper() == "INACTIVE")
                div2.InnerHtml += "<td width='22%' align='left' class='inactive'>" + textarray[0] + "</td>";

            div2.InnerHtml += "<td width='25%' align='left'>" + textarray[1] + "</td>";

            string temp = entry.link.NavigateUrl;
            int index = temp.LastIndexOf("/");
            temp = temp.Substring(index + 1);

            div2.InnerHtml += "<td width='18%' align='center'><a href='" + temp + "'>" + textarray[2] + "</a></td>";
            div2.InnerHtml += "<td width='14%' align='center'>" + textarray[3] + "</td>";
            ToolboxDataContext tdc = new ToolboxDataContext();
            Property _property = Property.GetProperty(_campaign.property_id);


            if (_property.allocated_status.ToString().Equals("1"))
                div2.InnerHtml += "<td width='7%' align='center'><a href='" + temp + "'><img src='images/buy.gif'></a></td>";
            else if (_property.allocated_status.ToString().Equals("2"))
                div2.InnerHtml += "<td width='7%' align='center'><a href='" + temp + "'><img src='images/list_saved.gif'></a></td>";
            div2.InnerHtml += "</tr>";
            div2.InnerHtml += "</table>";
            containerDiv.Controls.Add(div2);
        }

        div1.Controls.AddAt(1, containerDiv);
        return div1;
    }
}

public class CampaingEntry
{
    private string campaignStauts;
    private int campaignID;
    public HyperLink link;
    private Campaign campaign;
    public const string archivedPage = "~/archives.aspx?task_num={0}";       // camapign id
    public const string activePage = "~/orders.aspx?task_num={0}";           // campaign id
    public const string inactivePage = "~/shoppingcart.aspx?task_num={0}";   // property id
    public static string CampaignDesFormat = "{0}; {1}; {2}; {3};";

    public CampaingEntry(Campaign _campaign)
    {
        this.campaignID = _campaign.campaign_id;
        this.campaignStauts = _campaign._CampaignStatus;
        this.campaign = _campaign;
    }

    public Control GetEntry()
    {
        //<p class=''><a href='#'></a></p>
        HtmlGenericControl entry = new HtmlGenericControl("p");
        entry.Attributes["class"] = "campaign_entry";
        link = new HyperLink();
        link.Text = String.Format(CampaignDesFormat, campaign._CampaignStatus, campaign.Agent.agent_name, campaignID, campaign.Orders.Count);
        switch(this.campaignStauts.ToLower())
        {
            case Campaign.STATUS_INACTIVE:
                link.NavigateUrl = String.Format(inactivePage, this.campaign.property_id);
                break;
            case Campaign.STATUS_CURRENT:
                link.NavigateUrl = String.Format(activePage, this.campaignID);
                break;
            case Campaign.STATUS_ARCHIVED:
                link.NavigateUrl = String.Format(archivedPage, this.campaignID);
                break;
        }
        entry.Controls.Add(link);        
        return entry;
    }
}
